﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.SafetyManagementEntity.Model;
using KingRoad.EPM.Web.Areas.SafetyConstruction.ViewModels;
using KingRoad.EPM.Web.Areas.SafetyConstruction.SafetyConstructionFramework.Common;

namespace KingRoad.EPM.Web.Areas.SafetyConstruction.Controllers
{
    public class ScmCheckChangedController : BaseController
    {

        /// <summary>
        /// 日常检查审批-审批列表 SafetyConstruction.ScmCheckChanged.CheckChangedList
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckChangedList(string pageName)
        {
            var model = new ScmDailycheckViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            //根据当前登录人 获取审批列表
            string sql = " Dc_State!=0 and Dc_State!=5 and Dc_BeCheckedSection in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
            string code = GetLoginUserOrganizeEntity.Code;
            switch (code)
            {
                case "SG":
                    sql += " and Dc_State=1 ";
                    break;
                case "ZDJL":
                    sql += " and Dc_State=2 ";
                    break;
                case "JC":
                    sql += " and Dc_State=2 and Dc_TypeCode='3' ";
                    break;
                case "JL":
                    sql += " and Dc_State=3 ";
                    break;
                case "YZ":
                    sql += " and Dc_State=4 ";
                    break;
                default:
                    sql += " and Dc_State=44444 ";
                    break;
            }
            model.TheDailycheckList = ScmDailycheckUIRule.GetDailycheckListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Dc_TimeStamp", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return String.IsNullOrEmpty(pageName) ? View(model) : View(pageName, model);
        }
        /// <summary>
        /// 日常检查审批-列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            var model = new ScmDailycheckViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return CheckChangedList("CheckChangedList");
            ViewBag.iput_dname = Request.Form["iput_dname"];
            ViewBag.iput_bdname = Request.Form["iput_bdname"];
            ViewBag.iput_timestart = Request.Form["iput_timestart"];
            ViewBag.iput_timeend = Request.Form["iput_timeend"];
            ViewBag.iput_bpro = Request.Form["iput_bpro"];
            //根据当前登录人 获取审批列表
            string sql = " Dc_State!=0 and Dc_State!=5 and Dc_BeCheckedSection in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
            string code = GetLoginUserOrganizeEntity.Code;
            switch (code)
            {
                case "SG":
                    sql += " and Dc_State=1 ";
                    break;
                case "ZDJL":
                    sql += " and Dc_State=2 ";
                    break;
                case "JC":
                    sql += " and Dc_State=2 and Dc_TypeCode='3' ";
                    break;
                case "JL":
                    sql += " and Dc_State=3 ";
                    break;
                case "YZ":
                    sql += " and Dc_State=4 ";
                    break;
                default:
                    sql += " and Dc_State=44444 ";
                    break;
            }
            //if (ViewBag.iput_dname != "")
            //    sql += " and Dc_CheckDepName like '%" + ViewBag.iput_dname + "%' ";
            if (ViewBag.iput_bdname != "")
                sql += " and Dc_BeCheckedDepName like '%" + ViewBag.iput_bdname + "%' ";
            if (ViewBag.iput_timestart != "")
                sql += " and Dc_CheckTime>='" + ViewBag.iput_timestart + "' ";
            if (ViewBag.iput_timeend != "")
                sql += " and Dc_CheckTime<='" + ViewBag.iput_timeend + "' ";
            if (ViewBag.iput_bpro != "")
                sql += " and Dc_BriefProblem like '%" + ViewBag.iput_bpro + "%' ";
            model.TheDailycheckList = ScmDailycheckUIRule.GetDailycheckListForPage(ViewBag.pageNum, ViewBag.numPerPage, "Dc_TimeStamp", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("CheckChangedList", model);
        }
        /// <summary>
        /// 日常检查审批-审批弹出框
        /// </summary>
        /// <returns></returns>
        public ActionResult CheckChangedApproval(Guid id)
        {
            ScmDailycheckInfo dInfo = ScmDailycheckUIRule.GetDailycheckInfo(id);
            int state = dInfo.Dc_State;

            var model = new ScmDailycheckViewModel();
            model.TheAuditInfo = new ScmAuditInfo();
            model.TheAuditInfo.A_ID = Guid.NewGuid().ToString();
            model.TheAuditInfo.A_PrimayID = id.ToString();
            model.TheAuditInfo.A_Time = DateTime.Now;
            model.TheAuditInfo.A_IsApproved = true;
            model.TheAuditInfo.A_State = state;
            model.TheAuditInfo.A_Remark = ScmDailycheckUIRule.GetDailycheckInfo(id).Dc_FeedbackInfo;
            //当前登陆人信息
            model.TheAuditInfo.A_DepID = GetLoginUserDepartmentId;
            model.TheAuditInfo.A_DepName = GetLoginUserDepartmentName;
            model.TheAuditInfo.A_ManID = GetLoginUserId;
            model.TheAuditInfo.A_ManName = GetLoginUserRealName;
            //1=施工单位，2=驻地办，3=中心试验室，4=总监办
            int type = Convert.ToInt32(dInfo.Dc_TypeCode);
            List<SelectListItem> list = new List<SelectListItem>();
            switch (state)
            {
                case 1:
                    //施工单位上报
                    list.Add(new SelectListItem { Text = "通过(请驻地办审批)", Value = "true" });
                    break;
                case 2:
                    //驻地办/中心试验室审批
                    list.Add(new SelectListItem { Text = "通过(请总监办审批)", Value = "true" });
                    if (type == 1)
                        list.Add(new SelectListItem { Text = "不通过(责令施工单位继续整改)", Value = "false" });
                    break;
                case 3:
                    //总监办审批
                    list.Add(new SelectListItem { Text = "通过(请安监部审批)", Value = "true" });
                    if (type == 1)
                        list.Add(new SelectListItem { Text = "不通过(责令施工单位继续整改)", Value = "false" });
                    if (type == 2)
                        list.Add(new SelectListItem { Text = "不通过(责令驻地办继续整改)", Value = "false" });
                    if (type == 3)
                        list.Add(new SelectListItem { Text = "不通过(责令中心试验室继续整改)", Value = "false" });
                    break;
                case 4:
                    //安监部审批
                    list.Add(new SelectListItem { Text = "通过(整改完成)", Value = "true" });
                    if (type == 1)
                        list.Add(new SelectListItem { Text = "不通过(责令施工单位继续整改)", Value = "false" });
                    if (type == 2)
                        list.Add(new SelectListItem { Text = "不通过(责令驻地办继续整改)", Value = "false" });
                    if (type == 3)
                        list.Add(new SelectListItem { Text = "不通过(责令中心试验室继续整改)", Value = "false" });
                    if (type == 4)
                        list.Add(new SelectListItem { Text = "不通过(责令总监办继续整改)", Value = "false" });
                    break;
                default:
                    break;
            }
            //获取模块ID
            ViewBag.MoudleID = GetEntityByUrl(Models.PageURL.整改审核);
            //附件目录名
            ViewBag.DirWhere = Models.DirName.整改审核;
            //附件
            var commonFileList = CommonFileUIRule.GetCommonFilesByConditon(dInfo.Assist_ID, "DailyCheck_Rules");//SafeKnowledge_Rules  BizType类型
            ViewBag.AssisId = dInfo.Assist_ID;
            ViewBag.FileIds = string.Join(",", commonFileList.Select(t => t.FILE_FILEID));
            ViewBag.DeleteIds = string.Join(",", commonFileList.Select(t => t.FILE_ID));

            ViewBag.auList = list;
            return View(model);
        }
        /// <summary>
        /// 日常检查-审批(提交)
        /// </summary>
        /// <param name="model">日常检查审批model</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CheckChangedApproval(ScmDailycheckViewModel model)
        {
            var aInfo = model.TheAuditInfo;
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                int state = 0;
                //日常检查信息
                ScmDailycheckInfo dInfo = ScmDailycheckUIRule.GetDailycheckInfo(new Guid(aInfo.A_PrimayID));
                //1=施工单位，2=驻地办，3=中心试验室，4=总监办
                int type = Convert.ToInt32(dInfo.Dc_TypeCode);
                switch (aInfo.A_State)
                {
                    case 1:
                        //施工单位上报
                        state = 2;
                        break;
                    case 2:
                        //驻地办/中心试验室审批
                        if (aInfo.A_IsApproved)
                            state = 3;
                        else
                            state = 1;
                        break;
                    case 3:
                        //总监办审批
                        if (aInfo.A_IsApproved)
                            state = 4;
                        else
                        {
                            if (type == 1)
                                state = 1;
                            if (type == 2||type == 3)
                                state = 2;
                        }
                        break;
                    case 4:
                        //安监部审批
                        if (aInfo.A_IsApproved)
                            state = 5;
                        else
                        {
                            if (type == 1)
                                state = 1;
                            if (type == 2 || type == 3)
                                state = 2;
                            if (type == 4)
                                state = 3;
                        }
                        break;
                    default:
                        break;
                }
                ScmAuditUIRule.AddAudit(aInfo, 1, state);
                //修改附件
                ScmDailycheckInfo sInfo = ScmDailycheckUIRule.GetDailycheckInfo(new Guid(aInfo.A_PrimayID));
                var AssistID = Convert.ToInt32(Request.Form["AssistID"]);
                sInfo.Assist_ID = AssistID;

                ScmDailycheckUIRule.EditDailycheck(sInfo);
            });
            dynamic jsonReturn = new ExpandoObject();
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "审批成功",
                    navTabId = "SafetyConstruction.ScmCheckChanged.CheckChangedList",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "审批失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
    }
}
